<template>
  <div class="proDialog">
    <el-dialog title="选择优惠券" :visible.sync="visible" width="800px" z-index="1500" append-to-body @close="toCancelBox">
      <el-form class="formList" :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch"
        label-width="80px" style="padding-top: 0">


        <el-form-item label="优惠券名称" prop="couponName">
          <el-input v-model="queryParams.couponName" placeholder="请输入优惠券名称" clearable size="small" />
        </el-form-item>




        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        </el-form-item>
      </el-form>

      <el-table border ref="indexTable" :data="infoList" tooltip-effect="dark" style="width: 100%"
        @selection-change="handleSelectionChange" :row-key="bindRowKeys">
        <el-table-column type="selection" align="center" :reserve-selection="true">
        </el-table-column>

        <el-table-column label="图标" align="center" prop="couponIconUrl">
          <template slot-scope="scope">
            <el-image v-if="scope.row.couponIconUrl != null && scope.row.couponIconUrl != '' " slot="reference"
              :src="scope.row.couponIconUrl" class="image-rows" :preview-src-list="[scope.row.couponIconUrl]">
            </el-image>
          </template>
        </el-table-column>
        <el-table-column label="优惠券名称" align="center" prop="couponName" :show-overflow-tooltip="true" min-width="120" >
        
        </el-table-column>
        <el-table-column label="类型" align="center" prop="couponType" :formatter="couponTypeFormat" />
        <el-table-column label="优惠内容" align="center" prop="couponAmount" :show-overflow-tooltip="true" width="150">
          <template slot-scope="scope">
            <span>{{ scope.row.amountStr }}</span>
          </template>
        </el-table-column>
  
        <el-table-column label="开始时间" align="center" prop="startDate" width="160" sortable>
          <template slot-scope="scope">
            <span>{{
                parseTime(scope.row.startDate, "{y}-{m}-{d} {h}:{i}:{s}")
              }}</span>
          </template>
        </el-table-column>
        <el-table-column label="结束时间" align="center" prop="endDate" width="160" sortable>
          <template slot-scope="scope">
            <span>{{
                parseTime(scope.row.endDate, "{y}-{m}-{d} {h}:{i}:{s}")
              }}</span>
          </template>
        </el-table-column>
        <el-table-column label="有效时长" align="center" prop="expireDays" :show-overflow-tooltip="true" />

      </el-table>

      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
        @pagination="getList" :page-sizes="pageSizess" />

      <div style="display:flex; justify-content: flex-end;">
        <el-button @click="determine" type="primary">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>

import {
    listCoupon,
    getCoupon,
    delCoupon,
    addCoupon,
    updateCoupon,
    updateStatus,
    getCouponRule,
    addCouponRule,
    updateCouponRule
  } from "@/api/activity/coupon";
export default {
  props: {
    flag: {
      type: Boolean,
    },

  },
  watch: {
    flag(val) { },
  },
  data() {
    return {
      // 显示搜索条件
      showSearch: true,
      datas: null,
      selectedDatas: [],
      pageSizess: [5, 10, 15, 20, 25, 30, 50],
      // 商品信息表格数据
      infoList: [],
      // indexTable: [],
      infoListHistory: [],
      multipleSelection: [],
      total: 0,
      queryParams: {
        pageNum: 1,
        pageSize: 5,
        storeName: null,
          dataStatus:'1',
      },
      visible: false,
      categoryOptions: []
    };
  },
  created() {
    this.visible = this.flag;
    this.getList();

  },
  methods: {
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    toCancelBox() {
      this.visible = false;
      this.$emit("toCloseBox", false);
    },
    //回显选中
    greet(selectedData) {
      this.selectedDatas = selectedData;
      this.selectedDatas.forEach((item) => {
        this.$refs.indexTable.toggleRowSelection(item, true);
      });
    },
    bindRowKeys(row) {
      return row.couponId;
    },
    handleSelectionChange(val) {
      this.datas = this.multipleSelection;
      this.multipleSelection = val
      if (this.datas.length > 0) {
        for (let i = 0; i < this.datas.length; i++) {
          for (let k = 0; k < this.multipleSelection.length; k++) {
            if (this.datas[i].couponId == this.multipleSelection[k].couponId) {
              this.multipleSelection[k] = this.datas[i]
            }
          }
        }
      }
    },
    /** 确定选择商品 */
    determine() {
      this.$emit("multipleSelection", this.multipleSelection);
      this.infoList.forEach((item) => {
        this.$refs.indexTable.toggleRowSelection(item, false);
      });
      this.$forceUpdate();
      this.open = false;
    },
    /** 查询商品信息列表 */
    getList() {

      listCoupon(this.queryParams).then((response) => {
        this.infoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
  },
};
</script>
<style scoped>
.determine {
  position: relative;
  color: #fff;
}

.pagination-container {
  position: relative;
}

.image-rows {
  width: 80px;
  height: 80px;
}
</style>
